#ifndef _WIFI_CONFIGURATION_AP_H_
#define _WIFI_CONFIGURATION_AP_H_

#include <string>
#include <vector>
#include <mutex>

#include <esp_event.h>
#include <esp_timer.h>
#include <esp_netif.h>
#include <esp_wifi_types_generic.h>


class WifiPovisioningBLE {
public:
    static WifiPovisioningBLE& GetInstance();
    void SetSsidPrefix(const std::string &&ssid_prefix);
    void SetLanguage(const std::string &&language);
    void Start();
    void Stop();
    void StartSmartConfig();

    std::string GetDeviceName();

    // Delete copy constructor and assignment operator
    WifiPovisioningBLE(const WifiPovisioningBLE&) = delete;
    WifiPovisioningBLE& operator=(const WifiPovisioningBLE&) = delete;

private:
    // Private constructor
    WifiPovisioningBLE();
    ~WifiPovisioningBLE();

    std::mutex mutex_;
    EventGroupHandle_t event_group_;
    std::string ssid_prefix_;
    std::string language_;
    std::string ssid_;
    std::string password_;
    esp_event_handler_instance_t instance_any_id_;
    esp_event_handler_instance_t instance_got_ip_;
    esp_timer_handle_t scan_timer_ = nullptr;
    bool is_connecting_ = false;

    void StartBLEProvisioning();
    void Save(const std::string &ssid, const std::string &password);

    // Event handlers
    static void WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
    static void SmartConfigEventHandler(void* arg, esp_event_base_t event_base, 
                                      int32_t event_id, void* event_data);
    esp_event_handler_instance_t sc_event_instance_ = nullptr;
};

#endif // _WIFI_CONFIGURATION_AP_H_
